﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class BookingDAO
    {
        DataServices m_BookingDAO = new DataServices();

        public DataTable GetAll()
        {
            m_BookingDAO.Load("GetAllBooking", null);
            return m_BookingDAO;
        }

        public DataTable GetById(String bookId)
        {
            SqlParameter param = new SqlParameter("@BookId", SqlDbType.VarChar, 6);
            param.Value = bookId;

            m_BookingDAO.Load("GetBookingById", param);
            return m_BookingDAO;
        }

        public DataTable GetByLikeId(String bookId)
        {
            SqlParameter param = new SqlParameter("@BookId", SqlDbType.VarChar, 6);
            param.Value = bookId;

            m_BookingDAO.Load("GetBookingByLikeId", param);
            return m_BookingDAO;
        }

        public DataTable GetWithCustomer()
        {
            m_BookingDAO.Load("GetBookingWithCustomer", null);
            return m_BookingDAO;
        }

        public DataTable GetWithCustomerCompleted()
        {
            m_BookingDAO.Load("GetBookingWithCustomerCompleted", null);
            return m_BookingDAO;
        }

        public DataTable GetWithCustomerUnCompleted()
        {
            m_BookingDAO.Load("GetBookingWithCustomerUnCompleted", null);
            return m_BookingDAO;
        }

        public int Add(BookingInfo info)
        {
            SqlParameter[] param = new SqlParameter[7];
            param[0] = new SqlParameter("@BookId", SqlDbType.VarChar, 6);
            param[0].Value = info.BookId;

            param[1] = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param[1].Value = info.CusId;

            param[2] = new SqlParameter("@BookTime", SqlDbType.DateTime);
            param[2].Value = info.BookTime;

            param[3] = new SqlParameter("@TablesId", SqlDbType.VarChar, 6);
            param[3].Value = info.TablesId;

            param[4] = new SqlParameter("@Quantity", SqlDbType.Int);
            param[4].Value = info.Quantity;

            param[5] = new SqlParameter("@State", SqlDbType.Int);
            param[5].Value = info.State;

            param[6] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[6].Value = info.Remark;

            return m_BookingDAO.ExecuteUpdate("AddBooking", param);
        }

        public int Update(BookingInfo info)
        {
            SqlParameter[] param = new SqlParameter[7];
            param[0] = new SqlParameter("@BookId", SqlDbType.VarChar, 6);
            param[0].Value = info.BookId;

            param[1] = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param[1].Value = info.CusId;

            param[2] = new SqlParameter("@BookTime", SqlDbType.DateTime);
            param[2].Value = info.BookTime;

            param[3] = new SqlParameter("@TablesId", SqlDbType.VarChar, 6);
            param[3].Value = info.TablesId;

            param[4] = new SqlParameter("@Quantity", SqlDbType.Int);
            param[4].Value = info.Quantity;

            param[5] = new SqlParameter("@State", SqlDbType.Int);
            param[5].Value = info.State;

            param[6] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[6].Value = info.Remark;

            return m_BookingDAO.ExecuteUpdate("UpdateBooking", param);
        }

        public int Delete(String bookId)
        {
            SqlParameter param = new SqlParameter("@BookId", SqlDbType.VarChar, 6);
            param.Value = bookId;

            return m_BookingDAO.ExecuteUpdate("DeleteBooking", param);
        }

        public DataRow AddNewRow()
        {
            return m_BookingDAO.NewRow();
        }

        public void AddNewBooking(DataRow m_Row)
        {
            m_BookingDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_BookingDAO.ExecuteNoneQuery() > 0;
        }
    }
}
